#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QString>
#include <string>
#include <iostream>
#include <fstream>
#include <QFile>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    filepath("D:/App/Qt/book/test25/test25/data/")
{
    ui->setupUi(this);

    //读取国家列表 写入ComboBox
    showCountry("country.txt");
    showProvince(0);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::showProvince(int index)
{
    if(index == -1) return;
    std::string file(filepath+std::to_string(index)+".txt");
    std::ifstream ifs(file);
    if(!ifs.is_open())
    {
        qDebug() << QString::fromStdString(file) << " open error!";
        exit(-1);
    }
    ui->cb2->clear();
    while(std::getline(ifs,file)) ui->cb2->addItem(QString::fromStdString(file));
    ifs.close();
}

void Widget::showCountry(std::string filename)
{
    std::string file(filepath+filename);
    std::ifstream ifs(file);
    if(!ifs.is_open())
    {
        qDebug() << QString::fromStdString(file) << " open error!";
        exit(-1);
    }
    ui->cb1->clear();
    while(std::getline(ifs,file)) ui->cb1->addItem(QString::fromStdString(file));
    ifs.close();
}



void Widget::on_cb1_activated(int index)
{
    if(index == -1) return;
    showProvince(index);
}

void Widget::on_pushButton_clicked()
{
    ui->label_out->setText(ui->cb1->currentText()+'|'+ui->cb2->currentText()+'|'+ui->spinBox->text());
}
